package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ProdParameter;
import com.yami.shop.bean.model.ProdParameterLang;
import com.yami.shop.common.bean.LangConfig;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.dao.ProdParameterMapper;
import com.yami.shop.manager.impl.LangManager;
import com.yami.shop.service.ProdParameterLangService;
import com.yami.shop.service.ProdParameterService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Citrus
 * @date 2021-11-01 16:50:52
 */
@Service
public class ProdParameterServiceImpl extends ServiceImpl<ProdParameterMapper, ProdParameter> implements ProdParameterService {

    @Autowired
    private ProdParameterMapper prodParameterMapper;
    @Autowired
    private ProdParameterLangService prodParameterLangService;
    @Autowired
    private ShopConfig shopConfig;
    @Autowired
    private LangManager langManager;

    @Override
    @Cacheable(cacheNames = "listParameterByProdId", key = "#prodId")
    public List<ProdParameter> listParameterAndLang(Long prodId) {
        List<ProdParameter> prodParameters = prodParameterMapper.listProdParameterByProdId(prodId);
        langManager.getProdParameterAndLang(prodParameters);
        return prodParameters;
    }

    @Override
    public List<ProdParameter> listParameterByProdId(Long prodId) {
        ProdParameterServiceImpl prodParameterService = (ProdParameterServiceImpl) AopContext.currentProxy();
        List<ProdParameter> prodParameters = prodParameterService.listParameterAndLang(prodId);
        langManager.handleProdParameterLang(prodParameters);
        return prodParameters;
    }

    @Override
    public void removeCacheByProdId(Long prodId) {
        List<String> keys = new ArrayList<>();
        keys.add("listParameterByProdId" + Constant.UNION +  prodId);
        RedisUtil.del(keys);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndUpdateParameter(List<ProdParameter> prodParameterList, Long prodId) {
        List<ProdParameter> dbProdParameterList = listParameterByProdId(prodId);
        if (CollUtil.isEmpty(dbProdParameterList) && CollUtil.isEmpty(prodParameterList)) {
            return;
        }
        // 先删除，再新增
//        List<Long> ids = dbProdParameterList.stream().map(ProdParameter::getProdParameterId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(dbProdParameterList)) {
            prodParameterMapper.removeByProdId(prodId);
        }

        // 保存参数
        for (ProdParameter prodParameter : prodParameterList) {
            prodParameter.setProdId(prodId);
        }
        saveBatch(prodParameterList);

        // 保存参数语言信息
        List<ProdParameterLang> prodParameterLangList = new ArrayList<>();
        for (ProdParameter prodParameter : prodParameterList) {
            checkProdParameterLang(prodParameter.getProdParameterLangList());
            for (ProdParameterLang prodParameterLang : prodParameter.getProdParameterLangList()) {
                prodParameterLang.setProdParameterId(prodParameter.getProdParameterId());
            }
            prodParameterLangList.addAll(prodParameter.getProdParameterLangList());
        }
        // 先删除再保存
        prodParameterLangService.insertBatch(prodParameterLangList);
    }

    private void checkProdParameterLang(List<ProdParameterLang> prodParameterLangList) {
        LangConfig configLang = shopConfig.getLang();
        boolean containsMaster = false;
        for (ProdParameterLang prodParameterLang : prodParameterLangList) {
            if (Objects.equals(prodParameterLang.getLang(), configLang.getLang())) {
                containsMaster = true;
            }
        }
        if (!containsMaster) {
            throw new YamiShopBindException("语言数据已更新，请重新录入商品信息");
        }
    }

    @Override
    public void removeByProdId(Long prodId) {
        prodParameterMapper.removeByProdId(prodId);
    }
}
